package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.model.Position;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IPositionService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.PositionVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;

@RestController
@RequestMapping("/position")
public class PositionController {
    @Resource
    IPositionService positionService;

    @Resource
    IUserService userService;

    @GetMapping("/insert")
    @ApiOperation(value = "插入位置信息", notes = "通过前端传输经度，纬度，距离目标点的最大距离进行操作")
    public boolean insert(Principal principal, PositionVo positionVo) {
        User user = userService.getByUsername(principal.getName());
        Position position = new Position()
                .setUserId(user.getId())
                .setLongitude(positionVo.getLongitude())
                .setLatitude(positionVo.getLatitude())
                .setMaxDistance(positionVo.getMaxDistance());
        return positionService.save(position);
    }

    @GetMapping("/maxdistance")
    @ApiOperation(value = "改变最远距离", notes = "通过前端传输maxdistance操作")
    public boolean changemaxdistance(Principal principal,Double maxdistance){
        User user = userService.getByUsername(principal.getName());
        QueryWrapper<Position> query = new QueryWrapper<>();
        query.eq("user_id",user.getId());
        Position position = positionService.getOne(query);
        position.setMaxDistance(maxdistance);
        return positionService.updateById(position);
    }

}
